package com.site.dao.impl;

import com.site.dao.ElectricWireSizeDao;
import com.site.pojo.ElectricWireSize;
import com.site.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * FileName: ElectricWireSizeDaoImpl.java
 * 电线规格表DaoImpl
 *
 * @author 田尚忠
 * @Date    2021/11/28
 * @version 1.00
 */
public class ElectricWireSizeDaoImpl implements ElectricWireSizeDao {

    /*查询全部*/
    @Override
    public List<ElectricWireSize> selectAll() throws SQLException {
        List<ElectricWireSize> list = new ArrayList<>();
        Connection conn = JDBCUtils.getConnection();
        String sql = "select `type_id`, `id`, `goods_id`, `with`, `height`, `length`, `describe` from electric_wire_size";
        ResultSet rs = JDBCUtils.executeQuery(conn, sql);
        while (rs.next()){
            ElectricWireSize electricWireSize = new ElectricWireSize();
            electricWireSize.setTypeId(rs.getLong("type_id"));
            electricWireSize.setId(rs.getLong("id"));
            electricWireSize.setGoodsId(rs.getLong("goods_id"));
            electricWireSize.setWith(rs.getString("with"));
            electricWireSize.setHeight(rs.getString("height"));
            electricWireSize.setLength(rs.getString("length"));
            electricWireSize.setDescribe(rs.getString("describe"));
            list.add(electricWireSize);
        }
        JDBCUtils.close(rs,null,conn);
        return list;
    }

    //按照id查询
    @Override
    public ElectricWireSize selectByid(int id) throws SQLException {
        Connection conn = JDBCUtils.getConnection();
        ElectricWireSize electricWireSize = new ElectricWireSize();
        String sql = "select `type_id`, `id`, `goods_id`, `with`, `height`, `length`, `describe` from electric_wire_size where id=?";
        ResultSet rs = JDBCUtils.executeQuery(conn, sql,id);
        while (rs.next()){
            electricWireSize.setTypeId(rs.getLong("type_id"));
            electricWireSize.setId(rs.getLong("id"));
            electricWireSize.setGoodsId(rs.getLong("goods_id"));
            electricWireSize.setWith(rs.getString("with"));
            electricWireSize.setHeight(rs.getString("height"));
            electricWireSize.setLength(rs.getString("length"));
            electricWireSize.setDescribe(rs.getString("describe"));
        }
        JDBCUtils.close(rs,null,conn);
        return electricWireSize;
    }
    //按照商品id查询
    @Override
    public List<ElectricWireSize> selectByGoodsid(int id) throws SQLException {
        List<ElectricWireSize> list = new ArrayList<>();
        Connection conn = JDBCUtils.getConnection();
        String sql = "select `type_id`, `id`, `goods_id`, `with`, `height`, `length`, `describe` from electric_wire_size where goods_id=?";
        ResultSet rs = JDBCUtils.executeQuery(conn, sql,id);
        while (rs.next()){
            ElectricWireSize electricWireSize = new ElectricWireSize();
            electricWireSize.setTypeId(rs.getLong("type_id"));
            electricWireSize.setId(rs.getLong("id"));
            electricWireSize.setGoodsId(rs.getLong("goods_id"));
            electricWireSize.setWith(rs.getString("with"));
            electricWireSize.setHeight(rs.getString("height"));
            electricWireSize.setLength(rs.getString("length"));
            electricWireSize.setDescribe(rs.getString("describe"));
            list.add(electricWireSize);
        }
        JDBCUtils.close(rs,null,conn);
        return list;
    }

    //没有事务的添加
    @Override
    public int addElectricWireSize(ElectricWireSize electricWireSize) {
        String sql ="INSERT INTO `hurricane_house`.`electric_wire_size` (`type_id`, `goods_id`, `with`, `height`, `length`, `describe`) VALUES (?,?, ?, ?, ?, ?);";
        Object [] params={electricWireSize.getTypeId(),electricWireSize.getGoodsId(),electricWireSize.getWith(),electricWireSize.getHeight(),electricWireSize.getLength(),electricWireSize.getDescribe()};
        return JDBCUtils.executeUpdate(sql,params);
    }
    //事务的添加
    @Override
    public int addElectricWireSize(Connection conn, ElectricWireSize electricWireSize) {
        String sql ="INSERT INTO `hurricane_house`.`electric_wire_size` (`type_id`, `goods_id`, `with`, `height`, `length`, `describe`) VALUES (?,?, ?, ?, ?, ?);";
        Object [] params={electricWireSize.getTypeId(),electricWireSize.getGoodsId(),electricWireSize.getWith(),electricWireSize.getHeight(),electricWireSize.getLength(),electricWireSize.getDescribe()};
        return JDBCUtils.executeUpdate(conn,sql,params);
    }

    //按照id删除
    @Override
    public int delElectricWireSizeByid(int id) {
        String sql = "DELETE FROM electric_wire_size WHERE id=?";
        Object [] params = {id};
        return JDBCUtils.executeUpdate(sql,params);
    }

    @Override
    public int delElectricWireSizeByid(Connection conn, int id) {
        String sql = "DELETE FROM electric_wire_size WHERE id=?";
        Object [] params = {id};
        return JDBCUtils.executeUpdate(conn,sql,params);
    }

    @Override
    public int delElectricWireSizeByGoodsid(int id) {
        String sql = "DELETE FROM electric_wire_size WHERE goods_id=?";
        Object [] params = {id};
        return JDBCUtils.executeUpdate(sql,params);
    }

    @Override
    public int delElectricWireSizeByGoodsid(Connection conn, int id) {
        String sql = "DELETE FROM electric_wire_size WHERE goods_id=?";
        Object [] params = {id};
        return JDBCUtils.executeUpdate(conn,sql,params);
    }

    @Override
    public int updateElectricWireSize(int id,ElectricWireSize electricWireSize) {
        String sql = "UPDATE `hurricane_house`.`electric_wire_size` SET `type_id` = ?, `goods_id` = ?, `with` = ?, `height` = ?, `length` = ?, `describe` = ? WHERE `id` = ?;";
        Object [] params={electricWireSize.getTypeId(),electricWireSize.getGoodsId(),electricWireSize.getWith(),electricWireSize.getHeight(),electricWireSize.getLength(),electricWireSize.getDescribe(),id};
        return JDBCUtils.executeUpdate(sql,params);
    }

    @Override
    public int updateElectricWireSize(Connection conn, int id, ElectricWireSize electricWireSize) {
        String sql = "UPDATE `hurricane_house`.`electric_wire_size` SET `type_id` = ?, `goods_id` = ?, `with` = ?, `height` = ?, `length` = ?, `describe` = ? WHERE `id` = ?;";
        Object [] params={electricWireSize.getTypeId(),electricWireSize.getGoodsId(),electricWireSize.getWith(),electricWireSize.getHeight(),electricWireSize.getLength(),electricWireSize.getDescribe(),id};
        return JDBCUtils.executeUpdate(conn,sql,params);
    }
}
